﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using NS_QuangCaoTiepThiBUS;

namespace NS_QuangCaoTiepThi.QCTT.Admin
{
    public partial class QuanLyNoiDung : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                LayDanhSachLoaiNoiDung();
            }
        }

        protected void TabContainer1_ActiveTabChanged(object sender, EventArgs e)
        {
            
        }

        //Table LoaiNoiDung
        protected void LayDanhSachLoaiNoiDung()
        {
            LoaiNoiDungBUS lndBUS = new LoaiNoiDungBUS();
            gvDanhSachLoaiNoiDung.DataSource = lndBUS.LayDanhSachLoaiNoiDung();
            gvDanhSachLoaiNoiDung.DataBind();
        }

        protected void btnThemLoaiNoiDung_Click(object sender, EventArgs e)
        {
            LoaiNoiDung lndDTO = new LoaiNoiDung();
            lndDTO.MaLoaiNoiDung = txtMaLoaiNoiDung.Text.ToUpper();
            lndDTO.TenLoaiNoiDung = txtTenLoaiNoiDung.Text.ToUpper();

            if (txtMaLoaiNoiDung.Text != string.Empty)
            {
                if (txtTenLoaiNoiDung.Text != string.Empty)
                {
                    LoaiNoiDungBUS lndBUS = new LoaiNoiDungBUS();
                    if (lndBUS.KiemTraTonTaiLoaiNoiDung(lndDTO) == false)
                    {
                        lndBUS.Them(lndDTO);
                    }
                    else
                    {
                        Alert.Show("Da ton tai ma loai noi dung nay");

                        return;
                    }
                }
                else
                {
                    Alert.Show("Ten loai noi dung khong duoc rong");

                    return;
                }
            }
            else
            {
                Alert.Show("Ma loai noi dung khong duoc rong");

                return;
            }

            Alert.Show("Da them thanh cong");

            LayDanhSachLoaiNoiDung();
        }

        protected void gvDanhSachLoaiNoiDung_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvDanhSachLoaiNoiDung.EditIndex = e.NewEditIndex;

            LayDanhSachLoaiNoiDung();
        }

        protected void gvDanhSachLoaiNoiDung_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtMaLoaiNoiDung = (TextBox)(gvDanhSachLoaiNoiDung.Rows[e.RowIndex].Cells[0].FindControl("txtMaLoaiNoiDung"));
            TextBox txtTenLoaiNoiDung = (TextBox)(gvDanhSachLoaiNoiDung.Rows[e.RowIndex].Cells[0].FindControl("txtTenLoaiNoiDung"));

            LoaiNoiDung lndDTO = new LoaiNoiDung();
            lndDTO.MaLoaiNoiDung = txtMaLoaiNoiDung.Text.ToUpper();
            lndDTO.TenLoaiNoiDung = txtTenLoaiNoiDung.Text.ToUpper();

            if (txtTenLoaiNoiDung.Text != string.Empty)
            {
                LoaiNoiDungBUS lndBUS = new LoaiNoiDungBUS();
                lndBUS.Sua(lndDTO);
            }
            else
            {
                Alert.Show("Ten loai noi dung khong duoc rong");

                return;
            }

            Alert.Show("Da sua thanh cong");

            gvDanhSachLoaiNoiDung.EditIndex = -1;

            LayDanhSachLoaiNoiDung();
        }

        protected void gvDanhSachLoaiNoiDung_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvDanhSachLoaiNoiDung.EditIndex = -1;

            LayDanhSachLoaiNoiDung();
        }

        protected void gvDanhSachLoaiNoiDung_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            LoaiNoiDung lndDTO = new LoaiNoiDung();
            lndDTO.MaLoaiNoiDung = gvDanhSachLoaiNoiDung.DataKeys[e.RowIndex].Value.ToString();

            LoaiNoiDungBUS lndBUS = new LoaiNoiDungBUS();
            lndBUS.Xoa(lndDTO);

            Alert.Show("Da xoa thanh cong");

            LayDanhSachLoaiNoiDung();
        }

        protected void gvDanhSachLoaiNoiDung_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDanhSachLoaiNoiDung.PageIndex = e.NewPageIndex;

            LayDanhSachLoaiNoiDung();
        }
    }
}